package com.main.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.generico.dao.ctg.CtgCatalogoDao;
import com.generico.dao.ctg.CtgProveedorDao;
import com.generico.dto.ctg.CtgProveedor;
import com.main.dao.InventarioDao;
import com.main.dao.SugerenciaDao;
import com.main.java.Inventario;
import com.main.java.Sugerencia;
import com.web.util.GenericoUtil;

@Controller
@RequestMapping("/empleado")
public class CtgProveedorController extends BaseController{
	@Autowired
	private InventarioDao inventarioDao;
		
	@Autowired
	private CtgCatalogoDao ctgCatalogoDao;
	
	@Autowired
	private CtgProveedorDao ctgProveedorDao;
	
	
	@RequestMapping(value = "/ctgProveedores")
	public String controlInventario(HttpServletRequest request, ModelMap model) {
		model.put("proveedores", JSONArray.fromObject(ctgProveedorDao.obtenerProveedorActivos()));
		model.put("userName", GenericoUtil.getUserName());
		model.put("usuarioId", GenericoUtil.getSgdUsuarioIdFromAcegi());
		model.put("rol", GenericoUtil.getRol().toString());
		return "empleado/CtgProveedor";
		
	}
	
	@RequestMapping(value = "/ctgProveedorSave", method = {RequestMethod.POST})
	public void save(HttpServletResponse response,
			@ModelAttribute(value = "ctgProveedor") CtgProveedor ctgProveedor) {
		Map<String, Object> result = new HashMap<String, Object>();
		try{
			
			if(ctgProveedor != null){
				
				
				
					ctgProveedorDao.guardarProveedor(ctgProveedor);
					
					result.put(SUCCESS, true);
					result.put(MESSAGE, "Datos de Proveedor Guadados Correctamente");
					List<Object[]> list = ctgProveedorDao.obtenerProveedorActivos();
					for (int index = 0; index < list.size(); index++) {
						if(ctgProveedor.getCtgProveedorId().equals(list.get(index)[0])){
							result.put("ctgProveedorIndex", index);
							break;
						}
					}
					result.put("ctgProveedores", JSONArray.fromObject(list));
				
				
				
				
			}else{
				result.put(FAILURE, true);
				result.put(MESSAGE, GenericoUtil.ERROR_MESSAGE);
			}
		}catch(Exception e){
			result.put(FAILURE, true);
			result.put(MESSAGE, GenericoUtil.getExceptionMessage(e));
		}
		serializeObject(result, response);
	}
}
	
	
	
	
	

